package com.douhuang.cloud.controller;

import cn.hutool.core.date.SystemClock;
import com.douhuang.cloud.dto.PayDTO;
import com.douhuang.cloud.entities.Pay;
import com.douhuang.cloud.resp.R;
import com.douhuang.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@Tag(name = "支付微服务模块",description = "支付CRUD") //swagger接口文档标签
public class PayController{
    @Resource
    PayService payService;
    @PostMapping(value = "/pay/add")
    @Operation(summary = "新增",description = "新增支付流水方法,json串做参数")  //swagger接口文档注解
    public R<String> addPay(@RequestBody Pay pay){
        System.out.println(pay.toString());
        int i = payService.add(pay);
        return R.success("成功插入记录，返回值："+i);
    }
    @DeleteMapping(value = "/pay/del/{id}")
    @Operation(summary = "删除",description = "删除支付流水方法")
    public R<Integer> deletePay(@PathVariable("id") Integer id) {
        int i = payService.delete(id);
        return R.success(i);
    }
    @PutMapping(value = "/pay/update")
    @Operation(summary = "修改",description = "修改支付流水方法")
    public  R<String> updatePay(@RequestBody PayDTO payDTO){
        Pay pay = new Pay();
        BeanUtils.copyProperties(payDTO, pay);

        int i = payService.update(pay);
        return R.success("成功修改记录，返回值："+i);

    }
    @GetMapping(value = "/pay/get/{id}")
    @Operation(summary = "按照ID查流水",description = "查询支付流水方法")
    public R<Pay> getById(@PathVariable("id") Integer id){
        if(id < 0) throw new RuntimeException("id不能为负数");
        try {
            TimeUnit.SECONDS.sleep(62);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Pay pay = payService.getById(id);
        return R.success(pay);
    }


    //全部查询getall作为家庭作业+
    @GetMapping(value = "/pay/get/list")
    @Operation(summary = "查看所有的支付流水",description = "查询所有支付流水方法")
    public R<List<Pay>> getById(){
        List<Pay> payList = payService.getAll();
        return R.success(payList);
    }


    @Value("${server.port}")
    private String port;



    @GetMapping(value = "/pay/get/info")
    public R<String> getInfoByConsul(@Value("${douhuang.info}") String douhuangInfo) {
        return R.success("端口号：" + port + "，douhuangInfo：" + douhuangInfo);
    }

}
